package com.cdtu.web;


import com.cdtu.entity.News;
import com.cdtu.entity.NewsColumn;
import com.cdtu.service.NewsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {
    @Autowired
    private NewsService newsService;

    /*更新*/

    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody News news){
        HashMap<String, Object> map = new HashMap<>();
        news.setNewsUpdateTime(new Date());
        int update = newsService.update(news);
        if (update > 0) {
            map.put("status", 200);
            map.put("msg", "新闻更新成功了");
        } else {
            map.put("status", 1201);
            map.put("msg", "新闻更新失败了");
        }
        return map;
    }


    @GetMapping("/getById/{newsId}")
    public News getByNewsId(@PathVariable Integer newsId){
       return newsService.getUpdateNewsById(newsId);
    }



    @PostMapping("/addNews")
    public Map<String, Object> add(@RequestBody News news){
        HashMap<String, Object> map = new HashMap<>();
        news.setNewsDate(new Date());
        news.setNewsCreateTime(new Date());
        news.setNewsState((byte)1);
        int delete = newsService.add(news);
        if (delete > 0) {
            map.put("status", 200);
            map.put("msg", "新闻添加成功");
        } else {
            map.put("status", 1201);
            map.put("msg", "新闻添加失败");
        }
        return map;
    }

    @GetMapping("/getColumnList")
    public List<NewsColumn> getColumnListName(){
        List<NewsColumn> columnList = newsService.getColumnListName();
        return  columnList;
    }




    @DeleteMapping("/delete/{newsId}")
    public Map<String, Object> delete(@PathVariable Integer newsId){
        HashMap<String, Object> map = new HashMap<>();
        int delete = newsService.deleteById(newsId);
        if (delete > 0) {
            map.put("status", 200);
            map.put("msg", "新闻删除成功");
        } else {
            map.put("status", 1201);
            map.put("msg", "新闻删除失败");
        }
        return map;
    }

    @GetMapping("/getNews/{newsId}")
    public Map<String,Object> getById(@PathVariable Integer newsId){
        return newsService.getNewsById(newsId);

    }

    @GetMapping({"/page/{pageNum}/{pageSize}",
            "/pageByColumnId/{pageNum}/{pageSize}/{columnId}",
            "/pageBySource/{pageNum}/{pageSize}/{newsSource}",
            "/pageByDate/{pageNum}/{pageSize}/{newsDate}"})
    public PageInfo<News> homePage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                   @PathVariable (required = false) Integer columnId,
                                   @PathVariable (required = false) String newsSource,
                                   @PathVariable (required = false) String newsDate) throws ParseException {
        PageInfo<News> homePage = newsService.getNewsTitle(pageNum,pageSize,columnId,newsSource,newsDate);
        return homePage;
    }

    @GetMapping("/yearList")
    public List<Map<String,Object>> getYearList(){
        List<Map<String,Object>> yearList = newsService.getYearList();
        return  yearList;
    }

    @GetMapping("/sourceList")
    public List<String> getSourceList(){
        List<String>sourceList = newsService.getSourceList();
        return  sourceList;
    }

    @GetMapping("/columnList")
    public List<Map<String,Object>> getColumnList(){
        List<Map<String,Object>> columnList = newsService.getColumnList();
        return  columnList;
    }




}
